home *** CD-ROM | disk | FTP | other *** search
- /*
- ** xel.c
- **
- ** Practical Algorithms for Image Analysis
- **
- ** Copyright (c) 1997 ????
- */
-
- /*
- ** XEL
- **
- ** driver employing manipulations of doubly linked lists in llist.c
- **
- ** ref:
- ** R. Sessions
- ** "Reusable Data Structures For C", sect. 7.1
- ** Prentice Hall, Englewood Cliffs, NJ, 1989;
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
-
- #include "ip.h"
-
- struct emptype
- {
- char ssnum[12]; /* empl social security number */
- char name[40]; /* empl name */
- };
-
-
- #define BUF_LENGTH 54
-
- #define START 's' /* action code */
-
-
- /* function prototypes */
- extern int rdemp(struct emptype *emp,char *code,FILE *file);
- extern void prlist(struct linklist *list);
- extern void add_to_list(struct linklist *newlist,struct emptype *emp);
- extern void init_emplist(struct linklist *emplist);
- extern void main(int argc,char **argv);
-
-
- /*
- ** read employee data from file emp.dat
- */
- int rdemp(struct emptype *emp, char *code, FILE *file)
- {
- char buf[BUF_LENGTH];
- int i;
-
-
- for(i=0; i<BUF_LENGTH; i++) *(buf+i) = '\0';
-
- if(fgets(buf, BUF_LENGTH, file) == NULL)
- return(0);
- sscanf(buf, "%12s%20s %c", emp->ssnum, emp->name, code);
- return(1);
- }
-
-
- /*
- ** display list
- */
- void prlist(struct linklist *list)
- {
- struct emptype *emp;
-
- llhead(list);
- if( ll_length(list) != 0 ) /* list not empty */
- {
- do
- {
- emp = (struct emptype *)list->clp->item;
- printf(" %s %s\n", emp->ssnum, emp->name);
-
- }while( llnext(list) == True );
- }
- }
-
-
-
- /*
- ** add item to list
- */
- void add_to_list(struct linklist *list, struct emptype *emp)
- {
-
- lltail(list); /* add to new list; if empty, init */
- lladd((char *)emp, list);
-
- }
-
-
- /*
- ** initialize list paramters
- */
- void init_emplist(struct linklist *emplist)
- {
-
- llzero(emplist); /* init empty list */
- llsetsize(sizeof(struct emptype), emplist);
- }
-
-
-
-
-
- void main(int argc, char *argv[])
- {
-
- struct linklist curemp;
- struct emptype employee, *emp=&employee;
- char code;
- int retval;
- FILE *file;
-
- printf("xel: test driver for linked list functions\n\n");
-
- printf("...memory stats:\n");
- printf("...size of struct emptype: %d\n", sizeof(struct emptype));
- printf("...size of ptr to emptype: %d\n", sizeof(struct emptype *));
- printf("...size of ptr to char: %d\n", sizeof(char *));
-
- printf("\n...initialize lists\n");
- init_emplist(&curemp); /* init list of employees */
-
- printf("...open data file\n");
- if( (file = fopen("emp.dat", "r")) == NULL )
- {
- puts("\nCould not open input file\n");
- exit(1);
- }
-
- /*
- ** scan file and update lists
- */
- printf("...read test data from file\n");
- while( (retval = rdemp(emp, &code, file)) != 0 )
- {
- printf("...name = %s, ssnum = %s, action code = %c\n",
- emp->name, emp->ssnum, code);
-
- if( code == START )
- add_to_list(&curemp, emp);
-
- }
- fclose(file);
-
- printf("\n"); /* display lists */
- printf("list of active employees:\n");
- prlist(&curemp);
-
- }
-